package com.cain.DontTapTheWhiteTile.bean;

import android.graphics.Color;

import com.cain.DontTapTheWhiteTile.tool.Constants;

/**
 * Created by Cain on 2016/6/14.
 * 白块的块
 */
public class Block {

    public static final int CLICKABLE_RIGHT = 0;   //能点击且正确
    public static final int CLICKABLE_WRONG = 1;   //能点击但是错误
    public static final int CLICKABLE_NOT = 2;   //不能点击

    public float left;   //最左边坐标
    public float top;   //最上面坐标
    public float right;   //最右边坐标
    public float bottom;   //最下面坐标

    public int color;   //颜色

    public int state;   //当前块的状态

    public Block() {
        left = 0;
        top = 0;
        right = 0;
        bottom = 0;
        color = Constants.BLOCK_WHITE;
        state = CLICKABLE_NOT;
    }

    public Block(float left, float top, float right, float bottom, int color, int state) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
        this.color = color;
        if (state == CLICKABLE_NOT || state == CLICKABLE_RIGHT || state == CLICKABLE_WRONG)
            this.state = state;
        else
            this.state = CLICKABLE_NOT;
    }

    /**
     * 判断点是否包含在这个矩形里面
     * @param x
     * @param y
     * @return
     */
    public boolean contains(float x, float y) {
        if (x >= left && x <= right && y <= top && y >= bottom)
            return true;
        else
            return false;
    }

    /**
     * 是否在该行
     * @param y
     * @return
     */
    public boolean inLine(float y) {
        if (y <= top && y >= bottom)
            return true;
        else
            return false;
    }

    /**
     * 获取顶点数组
     * @return
     */
    public float[] getPointArray() {
        float[] result = new float[12];
        result[0] = left;
        result[1] = top;
        result[2] = 0;
        result[3] = right;
        result[4] = top;
        result[5] = 0;
        result[6] = left;
        result[7] = bottom;
        result[8] = 0;
        result[9] = right;
        result[10] = bottom;
        result[11] = 0;
        return result;
    }

    /**
     * 获取颜色数组
     * @return
     */
    public float[] getColorArray() {
        float red = (float)Color.red(color) / 255.0f;
        float green = (float)Color.green(color) / 255.0f;
        float blue = (float)Color.blue(color) / 255.0f;
        float alpha = 1.0f;
        float[] result = new float[16];
        for (int i = 0; i < result.length;) {
            result[i++] = red;
            result[i++] = green;
            result[i++] = blue;
            result[i++] = alpha;
        }
        return result;
    }

    /**
     * 获得右边的线条
     * @return
     */
    public float[] getRightLine() {
        float[] result = new float[6];
        result[0] = right;
        result[1] = top;
        result[2] = 0;
        result[3] = right;
        result[4] = bottom;
        result[5] = 0;
        return result;
    }

    /**
     * 获得底部的线条
     * @return
     */
    public float[] getBottomLine() {
        float[] result = new float[6];
        result[0] = left;
        result[1] = bottom;
        result[2] = 0;
        result[3] = right;
        result[4] = bottom;
        result[5] = 0;
        return result;
    }

    /**
     * 移动distance的距离
     * @param horizontalDis 水平方向距离
     * @param verticalDis 竖直方向距离
     */
    public void move(float horizontalDis, float verticalDis) {
        left += horizontalDis;
        right += horizontalDis;
        top += verticalDis;
        bottom += verticalDis;
    }

    /**
     * 左下角移动到
     * @param x
     * @param y
     */
    public void moveTo(float x, float y) {
        float horizontalDis = x - left;
        float verticalDis = y - bottom;
        move(horizontalDis, verticalDis);
    }
}
